//
//  NewestLabels.m
//  SoundPill
//
//  Created by 宋旭东 on 15/11/27.
//  Copyright © 2015年 宋旭东. All rights reserved.
//

#import "NewestLabels.h"

@interface NewestLabels ()
{
    UILabel *title;
    
    NSArray *_topicArray;
}
@end

@implementation NewestLabels
- (instancetype)initWithFrame:(CGRect)frame TopicCount:(NSInteger)topicCount
{
    self = [super initWithFrame:frame];
    if (self) {
        self.userInteractionEnabled = YES;
        [self initTopicLabelWith:topicCount];
    }
    return self;
}
- (void)initTopicLabelWith:(NSInteger)LabelCount
{
    title = [[UILabel alloc] init];
    [self addSubview:title];

    for (int i = 0; i < LabelCount; i++) {
       UILabel *label = [[UILabel alloc] init];
        label.tag = 123 + i;
        [self addSubview:label];
//        [self subviews];
        UIButton *publishButton = [UIButton buttonWithType:UIButtonTypeCustom];
        publishButton.tag = 134 + i;
        [self addSubview:publishButton];
    }
    
}
- (void)layoutTopicLabel:(NSArray *)topicArray
{
    _topicArray = topicArray;
    
//    NSArray *subviews = self.subviews;
    title.frame = CGRectMake(15, 5, self.frame.size.width - 30, self.frame.size.height / (topicArray.count + 1));
//    title.backgroundColor = [UIColor redColor];
    title.font = [UIFont boldSystemFontOfSize:17];
    title.text = @"最新话题:";
    title.textAlignment = NSTextAlignmentLeft;
    for (int i = 0; i < topicArray.count; i++ ) {
        UILabel *topiclabel = [self viewWithTag:123+i];
        topiclabel.userInteractionEnabled = YES;
        topiclabel.frame = CGRectMake(15, (self.frame.size.height - 10) / (topicArray.count + 1) * (i + 1), self.frame.size.width - 30, (self.frame.size.height - 10) / (topicArray.count + 1));
        topiclabel.text = [NSString stringWithFormat:@"#%@#", [topicArray[i] valueForKey:@"name"]];
        topiclabel.font = [UIFont systemFontOfSize:15];
        UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(topicSearch:)];
        [topiclabel addGestureRecognizer:recognizer];
        
        UIButton *publishButton = [self viewWithTag:134+i];
        publishButton.frame = CGRectMake(self.frame.size.width - 30, self.frame.size.height / (topicArray.count + 1) * (i + 1), 20, 20);
        publishButton.center = CGPointMake(self.frame.size.width - 20, topiclabel.center.y);
        [publishButton addTarget:self action:@selector(topicPublish:) forControlEvents:UIControlEventTouchUpInside];
        [publishButton setBackgroundImage:[UIImage imageNamed:@"add"] forState:UIControlStateNormal];
    }
}
- (void)topicPublish:(UIButton *)sender
{
    NSInteger index = sender.tag - 134;
    NSString *topicString = [_topicArray[index] valueForKey:@"name"];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"topicPublish" object:topicString];
}
- (void)topicSearch:(UITapGestureRecognizer *)sender
{
    UILabel *label = (UILabel *)sender.view;
    NSInteger index = label.tag - 123;
    NSString *topicString = [NSString stringWithFormat:@"#%@#",[_topicArray[index] valueForKey:@"name"]];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"searchTopic" object:topicString];
    
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
